package zy1213;

import cn.itcast.xml.XmlUtils;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Created by dingw on 2019/12/11.
 */
public class Demo {
    static boolean Boolean = false;
    static String password = "";
    static List<User> users = XmlUtils.readAll("user.xml", User.class);
    static List<Category> categories = XmlUtils.readAll("category.xml",Category.class);
    static List<Product> products = XmlUtils.readAll("product.xml",Product.class);
    static List<ProductDetails> productDetails = XmlUtils.readAll("productDetails.xml",ProductDetails.class);
    static HashMap<String, String> map = new HashMap<>();
    static List<shopcar> shopcars = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        System.out.println("===========================商城========================");
        System.out.println("===================================================");
        while (true){
            System.out.println("请输入要操作的选项");
            System.out.println("1.【注册】");
            System.out.println("2.【登录】");
            System.out.println("3.【忘记密码】");
            System.out.println("4.【查看商品类别信息】");
            System.out.println("5.【查看商品信息】");
            System.out.println("6.【查询商品信息】");
            System.out.println("7.【立即购买】");
            System.out.println("8.【查看购物车】");
            System.out.println("9.【退出】");
            int i = new Scanner(System.in).nextInt();
            switch (i){
                case 1:
                    zhuce();
                    break;
                case 2:
                    denglu();
                    break;
                case 3:
                    xgPassword();
                    break;
                case 4:
                    sptype();
                    break;
                case 5:
                    sp();
                    break;
                case 6:
                    cx();
                    break;
                case 7:
                    shop();
                    break;
                case 8:
                    shopcar();
                    break;
                case 9:
                    return;
            }
        }
    }
    /**
     * 结算购物车
     */
    public static void jiesuan() throws IOException {
        double money = 0,moneysum = 0,money1 = 0;
        String name = "";
        for (User user : users) {
            if (user.getPassword().equals(password)){
                money = user.getMoney();
                name = user.getUsername();
                break;
            }
        }
        for (shopcar s : shopcars) {
            for (ProductDetails detail : productDetails) {
                if (s.getId().equals(detail.getId())){
                    moneysum += detail.getPrice();
                }
            }
        }
        if (money>moneysum ){
            money1 = money-moneysum;
            for (User user : users) {
                if (user.getUsername().equals(name)){
                    user.setMoney(money1);
                }
            }
            XmlUtils.write("user.xml",users,false);
            for (shopcar s : shopcars) {
                for (ProductDetails detail : productDetails) {
                    if (s.getId().equals(detail.getId())){
                        detail.setNum(detail.getNum()-s.getNum());
                    }
                }
            }
            XmlUtils.write("productDetails.xml",productDetails,false);
            Iterator<shopcar> it = shopcars.iterator();

//            XmlUtils.write("orders.txt",shopcars,false);
            StringBuilder builder = new StringBuilder();
            for (shopcar s : shopcars) {
                for (ProductDetails detail : productDetails) {
                    for (Product product : products) {
                        if (s.getId().equals(detail.getId()) && detail.getProductId().equals(product.getId())){
                            builder.append(s.getName()+":"+product.getPname()+","+s.getNum()+","+detail.getPrice()+",待发货\r\n");
                        }
                    }
                }
            }
            FileUtils.writeStringToFile(new File("orders.txt"), builder.toString(),false);
            System.out.println("结算成功");
        }else {
            System.out.println("金额不足，请选择其他支付方式...");
        }

    }

    /**
     * 删除购物车中指定商品
     */
    public static void shanchu(){
        for (shopcar s : shopcars) {
            System.out.println(s.getName()+"  "+s.getId()+"  "+s.getNum());
        }
        boolean b = false;
        System.out.println("请输入要删除的商品id");
        Scanner sc = new Scanner(System.in);
        String id = sc.nextLine();
        for (int i = 0; i < shopcars.size(); i++) {
            if (shopcars.get(i).getId().equals(id)){
                shopcars.remove(i);
                b = true;
                break;
            }
        }
        if (b==true){
            XmlUtils.write("cart.xml",shopcars,false);
            System.out.println("删除成功");
            return;
        }else {
            System.out.println("该购物车中没有该商品");
            return;
        }
    }

    /**
     * 修改购物车指定商品数量
     */
    public static void xg(){
        for (shopcar s : shopcars) {
            System.out.println(s.getName()+"  "+s.getId()+"  "+s.getNum());
        }
        boolean b = false;
        System.out.println("请输入要修改的商品id");
        String id = new Scanner(System.in).nextLine();
        int index = 0;
        for (int i = 0; i < shopcars.size(); i++) {
            if (shopcars.get(i).getId().equals(id)){
                index = i;
                b = true;
                break;
            }
        }
        if (b==true){
            System.out.println("请输入修改后的商品数量");
            int num = new Scanner(System.in).nextInt();
            for (int i = 0; i < shopcars.size(); i++) {
                if (i==index){
                    shopcars.set(i,new shopcar(shopcars.get(i).getName(),shopcars.get(i).getId(),num));
                }
            }
            XmlUtils.write("cart.xml",shopcars,false);
            System.out.println("修改成功");
            return;
        }else {
            System.out.println("该购物车中没有该商品");
            return;
        }
    }

    /**
     * 添加购物车
     */
    public static void add(){
        String name = "";
        for (User user : users) {
            if (user.getPassword().equals(password)){
                name = user.getUsername();
                break;
            }
        }
        System.out.println("输入商品id");
        String id = new Scanner(System.in).nextLine();
        boolean b = false;
        if (shopcars.equals("")){
            b = false;
        }
        for (shopcar s : shopcars) {
            if (s.getId().equals(id)){
                b = true;
            }
        }
        if (b==true){
            System.out.println("购物车已有该商品，请重新输入");
            return;
        }
        boolean b1 = false;
        for (ProductDetails detail : productDetails) {
            if (detail.getId().equals(id)){
                b1 = true;
            }
        }
        if (b1==false){
            System.out.println("没有该商品，请重新输入");
            return;
        }
        System.out.println("输入要购买的商品数量");
        int num = new Scanner(System.in).nextInt();
        boolean b2 = false;
        for (ProductDetails detail : productDetails) {
            if (detail.getNum()>=num&&detail.getNum()>0){
                b2 = true;
            }
        }
        if (b2==true){
            System.out.println("库存不足，请重新输入");
            return;
        }
        shopcars.add(new shopcar(name,id,num));
        XmlUtils.write("cart.xml",shopcars,true);
        System.out.println("添加成功");
        return;
    }

    /**
     * 购物车
     */
    public static void shopcar() throws IOException {
        System.out.println("=============购物车=======================");
        System.out.println("1.【添加到购物车】");
        System.out.println("2.【修改购物车指定商品数量】");
        System.out.println("3.【删除购物车中指定商品】");
        System.out.println("4.【结算购物车】");
        System.out.println("5.【退出】");
        int s = new Scanner(System.in).nextInt();
        if (Boolean==false){
            System.out.println("请登录用户");
            return;
        }
        switch (s){
            case 1:
                add();
                break;
            case 2:
                xg();
                break;
            case 3:
                shanchu();
                break;
            case 4:
                jiesuan();
                break;
            case 5:
                return;
        }

    }

    /**
     * 立即购买
     */
    public static void shop() throws IOException {
        if (Boolean==false){
            System.out.println("请登录用户");
            return;
        }
        System.out.println("请输入要购买的货物id");
        Scanner sc = new Scanner(System.in);
        String id = sc.nextLine();
        boolean b = false;
        for (ProductDetails detail : productDetails) {
            if (detail.getId().equals(id)){
                 b=true;
                 break;
            }
        }
        System.out.println("请输入购买数量");
        String num = sc.nextLine();
        int num1 = 0;
        if (b==true){
            boolean b1 = false;
            for (ProductDetails detail : productDetails) {
                if (detail.getId().equals(id)&&detail.getNum()>=Integer.parseInt(num)){
                    b1=true;
                    num1 = detail.getNum()-Integer.parseInt(num);
                    break;
                }
            }
            if (b1==true){
                boolean b2 = false;
                double money = 0,money1 = 0,pmoney=0;
                for (User user : users) {
                    if (user.getPassword().equals(password)){
                        money = user.getMoney();
                        break;
                    }
                }
                for (ProductDetails detail : productDetails) {
                    if (detail.getId().equals(id)&&detail.getPrice()<=money){
                        b2=true;
                        pmoney = detail.getPrice();
                        money1 = money-detail.getPrice();
                        break;
                    }
                }
                if (b2==true){
                    String uname ="";
                    for (User user : users) {
                        if (user.getPassword().equals(password)){
                            user.setMoney(money1);
                            uname = user.getUsername();
                            break;
                        }
                    }
                    XmlUtils.write("user.xml",users,false);
                    String pid = "";
                    for (ProductDetails detail : productDetails) {
                        if (detail.getId().equals(id)){
                            pid = detail.getProductId();
                            detail.setNum(num1);
                            break;
                        }
                    }
                    XmlUtils.write("productDetails.xml",productDetails,false);
                    String pname = "";
                    for (Product product : products) {
                        if (product.getId().equals(pid)){
                            pname = product.getPname();
                            break;
                        }
                    }
                    String  str = uname+":"+pname+","+num+","+pmoney+",待发货";
                    FileUtils.writeStringToFile(new File("orders.txt"), str,true);
                    System.out.println("购买成功");
                }else {
                    System.out.println("金额不足，请选择其他支付方式...");
                    shop();
                }
            }else {
                System.out.println("库存不足，请重新选择");
            }
        }else {
            System.out.println("没有该商品id，请重新选择");
            shop();
        }
    }

    /**
     * 根据商品类别id查询商品信息
     */
    public static void cx(){
        sptype();
        System.out.println("请选择商品类别id");
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        System.out.println("商品类别id\t商品id\t商品名称");
        for (Product product : products) {
             if (product.getCid().equals(s)){
                 System.out.println(product.getCid()+"\t\t\t"+product.getId()+"\t\t"+product.getPname());
             }
        }
        System.out.println("请选择商品id");
        String s1 = sc.nextLine();
        System.out.println("商品颜色\t商品货物id\t内存大小\t" + "库存\t商品价格\t商品id");
        for (ProductDetails detail : productDetails) {
            if (detail.getProductId().equals(s1)){
                System.out.println(detail.getColor()+"\t\t"+detail.getId()+"\t\t\t"+detail.getMemory()
                        +"\t\t"+detail.getNum()+"\t\t"+detail.getPrice()+"\t\t"+detail.getProductId());
            }
        }
    }

    /**
     * 查看商品信息
     */
    public static void sp(){
        System.out.println("商品类别id\t商品id\t商品名称");
        for (Product product : products) {
            System.out.println(product.getCid()+"\t\t\t"+product.getId()+"\t\t"+product.getPname());
        }
    }

    /**
     * 查看商品类别信息
     */
    public static void sptype(){
        System.out.println("商品类别id\t商品类别名称");
        for (Category category : categories) {
            System.out.println(category.getCname()+"\t\t"+category.getId());
        }
    }

    /**
     * 忘记密码
     */
    public static void xgPassword(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String uname = sc.nextLine();
        boolean b=false;
        for (User user : users) {
            if (user.getUsername().equals(uname)){
                b=true;
            }
        }
        if (b){
            System.out.println("请输入手机号");
            String uphone = sc.nextLine();
            boolean b1=false;
            for (User user : users) {
                if (user.getPhone().equals(uphone)){
                    b1=true;
                }
            }
            if (b1){
                System.out.println("请输入密码");
                String upd1 = sc.nextLine();
                boolean b3 = false;
                for (User user : users) {
                    if (user.getPassword().equals(upd1)){
                        System.out.println("不能与正在使用的密码一致");
                        b3=true;
                        xgPassword();
                    }
                }
                if (b3==false){
                    System.out.println("请再次输入密码");
                    String upd2 = sc.nextLine();
                    if (!upd1.equals(upd2)){
                        System.out.println("两次密码不一致，请重新输入");
                        zhuce();
                    }
                    for (int i = 0; i < users.size(); i++) {
                        if (users.get(i).getUsername().equals(uname)){
                            users.set(i,new User(users.get(i).getId(),users.get(i).getUsername(),
                                    upd1,users.get(i).getPhone(),users.get(i).getRole(),users.get(i).getMoney()));
                        }
                    }
                }
            }else {
                System.out.println("该手机号错误...");
                xgPassword();
            }
        }else {
            System.out.println("没有该用户，请注册...");
            xgPassword();
        }
        XmlUtils.write("user.xml",users,false);
        System.out.println("密码修改成功");
    }

    /**
     * 登录
     */
    public static void denglu(){
        Date date = new Date();
        long t1 = date.getTime();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名或手机号");
        String str = sc.nextLine();
        boolean b=false;
        for (User user : users) {
            if (user.getUsername().equals(str)||user.getPhone().equals(str)){
                b=true;
                break;
            }
        }
        Date date1 = new Date();
        long t2 = date1.getTime();
        if ((t2-t1)/1000/60>5){
            System.out.println("登录超时，请重新登录");
            denglu();
        }else {
            if (b){
                System.out.println("请输入密码");
                String upd = sc.nextLine();
                boolean b1 =false;
                for (User user : users) {
                    if (user.getPassword().equals(upd)){
                        System.out.println("登录成功");
                        b1=true;
                        password = upd;
                        Boolean = true;
                        return;
                    }
                }
                if (b){
                    System.out.println("密码不正确，请重新输入");
                    denglu();
                }
            }else {
                System.out.println("用户名或手机号不存在，请重新输入");
                denglu();
            }
        }

    }

    /**
     * 注册
     */
    public static void zhuce(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String uname = sc.nextLine();
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getUsername().equals(uname)){
                System.out.println("用户名已存在，请重新注册");
                zhuce();
            }
        }
        System.out.println("请输入手机号");
        String uphone = sc.nextLine();
        if (uphone.length()<11){
            System.out.println("请输入11位数的手机号");
            zhuce();
        }
//        try {
//            int upone1 = Integer.parseInt(uphone);
//        } catch (NumberFormatException e) {
//            System.out.println("请输入数字");
//            zhuce();
//        }
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getPhone().equals(uphone)){
                System.out.println("手机号已存在，请重新注册");
                zhuce();
            }
        }
        System.out.println("请输入密码");
        String upd1 = sc.nextLine();
        System.out.println("请再次输入密码");
        String upd2 = sc.nextLine();
        if (!upd1.equals(upd2)){
            System.out.println("两次密码不一致，请重新输入");
            zhuce();
        }
        System.out.println("请输入小金库数额");
        double num = new Scanner(System.in).nextDouble();
        users.add(new User(users.size()+1+"",uname,upd1,uphone,"普通用户",num));
        XmlUtils.write("user.xml",users,false);
        System.out.println("注册成功");
    }
}
